﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.SoldierArchives.Model;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;
using System.Collections.Generic;
using Yipusoft.Common.BLL;


public partial class Police_TransfeSoldier : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400106"))
            {

                if (!string.IsNullOrEmpty(Request["ID"]))
                {

                    string sid = Request["ID"];
                    Soldier soldier = new Soldier();
                    SoldierArchiveInfo soldierInfo = soldier.GetSoldierInfoByID(sid);
                    BindSoldier(soldierInfo);
                    UserInfo member = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
                    //OldDepartmentList.DataSource = GetDepartmentList(member.DepartmentID, "203001", "203002", "203003");
                    //OldDepartmentList.DataTextField = "FullName";
                    //OldDepartmentList.DataValueField = "DepartmentID";
                    //OldDepartmentList.DataBind();
                    //if (soldierInfo.DepartmentID != "")
                    //    OldDepartmentList.SelectedValue = soldierInfo.DepartmentID;
                    lblOldDepartmentID.Text = soldierInfo.DepartmentID;
                    lblOldDepartmentName.Text = soldierInfo.DepartmentName;
                    //内部调动部门
                    DepartmentList.DataSource = GetDepartmentList(member.DepartmentID, "203001", "203002", "203003");
                    DepartmentList.DataTextField = "FullName";
                    DepartmentList.DataValueField = "DepartmentID";
                    DepartmentList.DataBind();

                    rblDCJingZhong.DataSource = CodeItem.GetList("ParentID='SBDD20' ORDER BY SeqID");
                    rblDCJingZhong.DataTextField = "ItemName";
                    rblDCJingZhong.DataValueField = "ItemID";
                    rblDCJingZhong.DataBind();
                    rblDCJingZhong.SelectedIndex = 0;

                    tyType.DataSource = CodeItem.GetList("parentID='SBDD40' order by seqID");
                    tyType.DataTextField = "ItemName";
                    tyType.DataValueField = "ItemID";
                    tyType.DataBind();
                    tyType.SelectedIndex = 0;

                    txtTransfeDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    txtReportDate.Text = DateTime.Now.AddDays(3).ToString("yyyy-MM-dd");
                    BindTransferList();
                    SetPanel();
                }
            }
            else if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400101") || Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400102"))
            {
                Response.Redirect("TransferSoldier1.aspx?RootMenuID=500000&ID=" + Request["ID"]);
            }
            else
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }
        }

    }

    public string GetSoldierID()
    {
        return Request.QueryString["ID"];
    }

    private void BindSoldier(SoldierArchiveInfo soldierInfo)
    {

        SoldierName.Text = soldierInfo.SoldierName;
        CardID.Text = soldierInfo.CardID;
        Sex.Text = soldierInfo.Sex;
        imgPhoto.ImageUrl = soldierInfo.Picture == "" ? "~/Images/NoPhoto.png" : "../../" + soldierInfo.Picture;
        DepartmentName.Text = soldierInfo.DepartmentName;
        NationName.Text = soldierInfo.NativeName;
        Birthday.Text = soldierInfo.Birthday == DateTime.MinValue ? "--" : soldierInfo.Birthday.ToString("yyyy-MM-dd");
        JoinDate.Text = soldierInfo.JoinTime == DateTime.MinValue ? "--" : soldierInfo.JoinTime.ToString("yyyy-MM-dd");
        RankName.Text = soldierInfo.RankName;
        DutyName.Text = soldierInfo.DutyName;
        SpecialityName.Text = soldierInfo.Speciality2 == "" ? soldierInfo.Speciality1Name : soldierInfo.Speciality1Name + "[" + soldierInfo.Speciality2Name + "]";
        //hlSoldierDetails.NavigateUrl = "~/BasicBase/SoldierArchive/SoldierView.aspx?RootMenuID=200000&ID=" + soldierInfo.SoldierID;
        //hlSoldierDetails.Target = "_blank";
    }



    private List<DepartmentInfo> GetDepartmentList(string departmentID, string permissionAdmin, string permissionLevel, string permissionUnit)
    {
        List<DepartmentInfo> departmentList = new List<DepartmentInfo>();
        if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionAdmin))
            departmentList = Department.GetList("(SUBSTRING(DepartmentID, 5, 3) = '000') OR (DepartmentID = '1313100') OR (DepartmentID = '1312100') or (DepartmentID = '1300017') or (DepartmentID = '1300038')or (DepartmentID = '1300035')");
        else

            if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionLevel))
                departmentList = Department.GetList("DepartmentID like '" + departmentID.Substring(0, 4) + "%'");
            else
                if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionUnit))
                    departmentList = Department.GetList("DepartmentID='" + departmentID + "'");

        return departmentList;
    }

    public List<DepartmentInfo> GetDepartmentList()
    {
        List<DepartmentInfo> departmentList = new List<DepartmentInfo>();
        departmentList = Department.GetList("Right(DepartmentID,2)='00'");
        return departmentList;
    }
    /// <summary>
    /// 内调
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnTransfer_Click(object sender, EventArgs e)
    {
        string id = Request["ID"];
        Soldier so = new Soldier();

        SoldierArchiveInfo soldier = so.GetSoldierInfoByID(id);
        TranslateInfo info = new TranslateInfo();
        info.DateArmy = soldier.JoinTime;
        info.DateLastModefy = DateTime.Now;
        info.FromDepartmentID = soldier.DepartmentID;
        info.FromJingZhong = "SBDD40";
        info.NewDepartment = DepartmentList.SelectedItem.Text;
        info.OldDepartment = soldier.DepartmentName;
        info.OldDepartment2 = soldier.DepartmentName;
        info.Ranker = soldier.RankName;
        info.Sex = soldier.Sex;
        info.SID = new Guid(id);
        info.SoldierName = soldier.SoldierName;
        info.TID = 3;
        info.ToDepartmentID = DepartmentList.SelectedValue;
        info.ToJingZhong = "SBDD40";
        Guid Fid = Translate.Insert(info);
        Response.Redirect("TranslateApprove/AppalyNew.aspx?Fid=" + Fid);
    }

    private void BindTransferList()
    {
        gvTransfer.DataSource = TransferSoldier.GetTransferListBySoldierID(Request["ID"]);
        gvTransfer.DataBind();
    }
    /// <summary>
    /// 调出
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnTransOut_Click(object sender, EventArgs e)
    {
        string id = Request["ID"];
        Soldier so = new Soldier();

        SoldierArchiveInfo soldier = so.GetSoldierInfoByID(id);
        TranslateInfo info = new TranslateInfo();
        info.DateArmy = soldier.JoinTime;
        info.DateLastModefy = DateTime.Now;
        info.FromDepartmentID = soldier.DepartmentID;
        info.FromJingZhong = "SBDD40";
        info.NewDepartment = txtDRDepartment.Text;
        info.OldDepartment = soldier.DepartmentName;
        info.OldDepartment2 = soldier.DepartmentName;
        info.Ranker = soldier.RankName;
        info.Sex = soldier.Sex;
        info.SID = new Guid(id);
        info.SoldierName = soldier.SoldierName;
        info.TID = 2;
        info.ToDepartmentID = "";
        info.ToJingZhong = rblDCJingZhong.SelectedValue;
        Guid Fid = Translate.Insert(info);
        Response.Redirect("TranslateApprove/AppalyNew.aspx?Fid=" + Fid);
    }
    protected void gvTransfer_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        bool canDelete = false;
        if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400101"))
            canDelete = true;
        else
            if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400102"))
            {
                if (gvTransfer.DataKeys[e.RowIndex].Values["TransferID"].ToString().Substring(0, 4) == Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID.Substring(0, 4))
                    canDelete = true;
            }
        if (canDelete)
        {
            if (TransferSoldier.DeleteTransfer(gvTransfer.DataKeys[e.RowIndex].Values["TransferID"].ToString(), User.Identity.Name, Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName, DateTime.Now))
            {
                lblInfo.Text = "删除调动记录成功。";
                BindTransferList();
            }
            else
                lblInfo.Text = "删除失败！";
        }
        else
            lblInfo.Text = "您没有删除此记录的权限!";
    }
    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
        SetPanel();
    }

    private void SetPanel()
    {
        this.Panel1.Visible = this.RadioButton1.Checked;

        this.Panel3.Visible = this.RadioButton3.Checked;
        this.Panel4.Visible = this.RadioButton4.Checked;
    }

    protected void gvTransfer_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (gvTransfer.PageIndex * gvTransfer.PageSize + e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");

        }
    }
    protected void btnTransOut1_Click(object sender, EventArgs e)
    {
        string transferID = "";
        if (!String.IsNullOrEmpty(Request["TID"]))
            transferID = Request["TID"];
        Soldier soldier = new Soldier();
        SoldierArchiveInfo soldierInfo = soldier.GetSoldierInfoByID(Request["ID"]);
        if (TransferSoldier.Transfer(Request["ID"], soldierInfo.DepartmentID, soldierInfo.DepartmentName, "", tyType.SelectedItem.Text,
            Convert.ToDateTime(txtTransfeDate.Text), User.Identity.Name, Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName,
            DateTime.Now, "SBDD40", tyType.SelectedValue, Convert.ToDateTime(txtReportDate.Text), txtApprover.Text, transferID) != Guid.Empty)
        {

            soldier.Delete(Request["ID"], "退役", DateTime.Now, User.Identity.Name, Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName);
            lblInfo.Text = "调出成功。";
            BindTransferList();
        }
        else
            lblInfo.Text = "调出失败！";
    }
}
